/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package protocol;

import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author Tiago
 */
public class TSServerInfo implements Serializable{

    public enum FIELD {
        UNIQUE_ID,
        SERVER_ADDRESS,
        SERVER_PORT,
        KEY_SIZE,
        USED_SIZE,
        AVAILABLE_SIZE,
        COMUNICATION_PORT
    }

    
    /* Detalhes de comunicacao */
    private Integer uniqueID;
    private String serverAddress;
    private int serverPort;

    private int comunicationPort;

    private long keySize;

    /* Tamanho usado e disponivel no servidor em bytes */
    private long usedSize;
    private long availableSize;

    private String connectionString;


    private volatile ArrayList<TSServerInfoListener> listeners;

    public TSServerInfo() {
         this.keySize = 0;
        this.usedSize = 0;
        this.availableSize = 0;
        this.comunicationPort=0;

        this.listeners = new ArrayList<TSServerInfoListener>();
    }




    public TSServerInfo( Integer uniqueID, String serverAddress, int comunicationPort,int serverPort ) {
        this();
        this.uniqueID = uniqueID;
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;
        this.comunicationPort = comunicationPort;
        generateConnectionString();
       

    }

    public void addListener(TSServerInfoListener l) {
        this.listeners.add(l);
    }

    public void removeListener(TSServerInfoListener l) {
        this.listeners.remove(l);
    }

    public void informListeners(FIELD field) {
        for( TSServerInfoListener l : listeners ) {
            l.onUpdate(this, field);
        }
    }


    
    public void updateUsedSize(int usedSizeInc) {
        this.usedSize += usedSizeInc;
        this.informListeners(FIELD.USED_SIZE);
    }

    public void updateAvailableSize(int availableSizeInc) {
        this.availableSize += availableSizeInc;
        this.informListeners(FIELD.AVAILABLE_SIZE);
    }



    
    /* GETTERS e SETTERS */

    public Integer getUniqueID() {
        return uniqueID;
    }

    public void setUniqueID(int uniqueID) {
        this.uniqueID = uniqueID;
        this.informListeners(FIELD.UNIQUE_ID);
    }


    public long getAvailableSize() {
        return availableSize;
    }

    public void setAvailableSize(long availableSize) {
        this.availableSize = availableSize;
        this.informListeners(FIELD.AVAILABLE_SIZE);
    }

    public long getKeySize() {
        return keySize;
    }

    public void setKeySize(long keySize) {
        this.keySize = keySize;
        this.informListeners(FIELD.KEY_SIZE);
    }

    public String getServerAddress() {
        return serverAddress;
    }

    public void setServerAddress(String serverAddress) {
        this.serverAddress = serverAddress;
        generateConnectionString();
        this.informListeners(FIELD.SERVER_ADDRESS);
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
        generateConnectionString();
        this.informListeners(FIELD.SERVER_PORT);
    }

    public void setComunicationPort(int comunicationPort) {
        this.comunicationPort = comunicationPort;
        this.informListeners(FIELD.COMUNICATION_PORT);
    }

    public int getComunicationPort() {
        return comunicationPort;
    }

    



    public long getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(long usedSize) {
        this.usedSize = usedSize;
        this.informListeners(FIELD.USED_SIZE);
    }

    public String getConnectionString() {
        return connectionString;
    }

   public void generateConnectionString(){
    connectionString = serverAddress+":"+serverPort;
   }

    public interface TSServerInfoListener {


        public void onUpdate(TSServerInfo server, FIELD field);

    }


}
